package com.ekuaitu.kuaitu.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.map.GeocodeSearchViewUtils;
import com.ekuaitu.kuaitu.utils.ImageLoader;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by Michael Pi on 2016/9/17.
 */
public class CarListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Map<String, String>> list_group = null;
    private Map<String, List<Map<String, String>>> map_child = null;
    private LayoutInflater inflater = null;

    public CarListAdapter(Context context, List<Map<String, String>> list_group, Map<String, List<Map<String, String>>> map_child) {
        this.context = context;
        this.list_group = list_group;
        this.map_child = map_child;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return list_group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String key = list_group.get(groupPosition).get("groupName");
        int size = map_child.get(key).size();
        return size;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list_group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = list_group.get(groupPosition).get("groupName");
        return map_child.get(key).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        View itemview1 = inflater.inflate(R.layout.item_carlist_group, parent, false);
        AutoUtils.autoSize(itemview1);
        TextView tv1 = (TextView) itemview1.findViewById(R.id.tx_list_group_locationName);
        TextView tv2 = (TextView) itemview1.findViewById(R.id.tx_list_group_areaName);
        TextView tv3 = (TextView) itemview1.findViewById(R.id.tx_list_group_distance);
        ImageView tx_list_group_arrow = (ImageView) itemview1.findViewById(R.id.tx_list_group_arrow);
        tv1.setText(list_group.get(groupPosition).get("locationName"));
        tv2.setText(list_group.get(groupPosition).get("areaName"));
        tv3.setText(list_group.get(groupPosition).get("distance"));
        if (isExpanded) {
            tx_list_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_details_up));
        } else {
            tx_list_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_details_down));
        }
        return itemview1;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        View itemview2 = inflater.inflate(R.layout.item_carlist_child, parent, false);
        AutoUtils.autoSize(itemview2);

        ImageView iv = (ImageView) itemview2.findViewById(R.id.iv_car_list_car);
        TextView tv1 = (TextView) itemview2.findViewById(R.id.tx_list_child_carPosition);
        TextView tv2 = (TextView) itemview2.findViewById(R.id.tx_car_list_brand);
        TextView tv3 = (TextView) itemview2.findViewById(R.id.tx_car_list_plate);
        TextView tv4 = (TextView) itemview2.findViewById(R.id.tx_car_list_duration);
        TextView tv5 = (TextView) itemview2.findViewById(R.id.tx_car_list_day);
        TextView tv6 = (TextView) itemview2.findViewById(R.id.tx_car_list_night);
        String key = list_group.get(groupPosition).get("groupName");
        Map info = map_child.get(key).get(childPosition);
        ImageLoader.getInstance().displayRoundImage(context, MyApplication.getInstance().getImageUrl() + info.get("carPicture"), iv);
        GeocodeSearchViewUtils geo = new GeocodeSearchViewUtils();
        tv1.setText("");
        geo.startGeocodeSearch(context, Double.parseDouble(info.get("carLatitude") + ""), Double.parseDouble(info.get("carLongitude") + ""), tv1);
        tv2.setText(info.get("carBrand") + "");
        tv3.setText(info.get("carLicense") + "");
        tv4.setText(info.get("carDuration") + "公里");
        tv5.setText(Double.parseDouble(info.get("dayPrice") + "") / 100 + "");
        tv6.setText(Double.parseDouble(info.get("nightPrice") + "") / 100 + "");
        return itemview2;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
